﻿using FB.Providers.EmployeeInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using FB.Extensions;

namespace FB.Providers
{
    public static class EmployeeInfoManager
    {
        //#region company 公司
        //public static Company GetCompany(string COMCode)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.GetCompany(COMCode);
        //}

        //public static List<Company> GetAllCompanies()
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.GetAllCompanies();
        //}

        //public static List<Company> FindCompanies(Expression<Func<Company, bool>> filter = null)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.FindCompanies(filter);
        //}

        //public static Company CreateCompany(Company company)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.CreateCompany(company);
        //}

        //public static void UpdateCompany(Company company)
        //{
        //    AspNetEmployeeInfoProviderManager.Default.UpdateCompany(company);
        //}

        //public static void DeleteCompany(string COMCode)
        //{
        //    AspNetEmployeeInfoProviderManager.Default.DeleteCompany(COMCode);
        //}
        //#endregion


        //#region Department 部門
        //public static Department GetDepartment(string DEPTCode)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.GetDepartment(DEPTCode);
        //}


        //public static List<Department> GetAllDepartments()
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.GetAllDepartments();
        //}

        //public static List<Department> FindDepartments(Expression<Func<Department, bool>> filter = null, string includeProperties = "")
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.FindDepartments(filter);
        //}

        //public static Department CreateDepartment(Department department)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.CreateDepartment(department);
        //}

        //public static void UpdateDepartment(Department department)
        //{
        //    AspNetEmployeeInfoProviderManager.Default.UpdateDepartment(department);
        //}

        //public static void DeleteDepartment(string DEPTCode)
        //{
        //    AspNetEmployeeInfoProviderManager.Default.DeleteDepartment(DEPTCode);
        //}
        //#endregion


        //#region Position 岗位
        //public static Position GetPosition(string POSCode)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.GetPosition(POSCode);
        //}

        //public static List<Position> GetAllPositions()
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.GetAllPositions();
        //}

        //public static List<Position> FindPositions(Expression<Func<Position, bool>> filter = null)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.FindPositions(filter);
        //}

        //public static Position CreatePosition(Position position)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.CreatePosition(position);
        //}

        //public static void UpdatePosition(Position position)
        //{
        //    AspNetEmployeeInfoProviderManager.Default.UpdatePosition(position);
        //}

        //public static void DeletePosition(string POSCode)
        //{
        //    AspNetEmployeeInfoProviderManager.Default.DeletePosition(POSCode);
        //}

        //#endregion


        //#region employee 員工
        //public static EmployeeInfoUnit EmployeeInfoUnit
        //{
        //    get
        //    {
        //        var EF = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
        //        if (null != EF)
        //        {
        //            return EF.GetEmployeeInfoUnit();
        //        }
        //        return null;
        //    }
        //}
        //public static string GenerateEMPCode(string EMPType)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.GenerateEMPCode(EMPType);
        //}

        //public static Employee GetEmployee(string EMPCode, string includeProperties = " Subordinates, Supervisors, PostDetails")
        //{
        //    var infoUnit = EmployeeInfoUnit;
        //    var query = infoUnit.EmployeeRepository.Get(t => t.EMPCode == EMPCode, null, includeProperties).ToList();
        //    return query.FirstOrDefault();
             
        //}

        //public static Employee GetEmployeeByUserKey(Guid userid, string includeProperties = " Subordinates, Supervisors, PostDetails")
        //{
        //    var infoUnit = EmployeeInfoUnit;
        //    var query = infoUnit.EmployeeRepository.Get(t => t.UserID == userid, null, includeProperties);
        //    return query.FirstOrDefault(); 
        //}
        //public static Employee GetEmployeeByUserName(string userName, string includeProperties = " Subordinates, Supervisors, PostDetails")
        //{
        //    var infoUnit = EmployeeInfoUnit;
        //    var query = infoUnit.EmployeeRepository.Get(t => t.UserName == userName, null, includeProperties);
        //    return query.FirstOrDefault(); 
           
        //}

        //public static Employee GetEmployeeByChineseName(string ChineseName, string includeProperties = " Subordinates, Supervisors, PostDetails")
        //{
        //    var infoUnit = EmployeeInfoUnit;
        //    var query = infoUnit.EmployeeRepository.Get(t => t.ChineseName == ChineseName, null, includeProperties);
        //    return query.FirstOrDefault(); 
          
        //}
        //public static List<Employee> GetAllEmployees(string includeProperties = " Subordinates, Supervisors, PostDetails")
        //{
        //    var infoUnit = EmployeeInfoUnit;
        //    var query = infoUnit.EmployeeRepository.Get(null, null, includeProperties);
        //    return query.ToList();
        //}

        //public static List<Employee> FindEmployees(Expression<Func<Employee, bool>> filter = null, string includeProperties = " Subordinates, Supervisors, PostDetails")
        //{
        //    var infoUnit = EmployeeInfoUnit;
        //    var query = infoUnit.EmployeeRepository.Get(filter, null, includeProperties);
        //    return query.ToList();
            
        //}

        //public static List<Employee> FindEmployees(
        //    Expression<Func<Employee, bool>> filter,
        //    Func<IQueryable<Employee>, IOrderedQueryable<Employee>> orderBy,
        //    int pageIndex, int pageSize, out int totalRecords, string includeProperties = "")
        //{
        //    var infoUnit = EmployeeInfoUnit;
        //    var query = infoUnit.EmployeeRepository.GetPagedList(filter, orderBy,pageIndex,pageSize,out totalRecords,  includeProperties);
        //    return query.ToList();
         
        //}

        //public static bool CreateEmployee(Employee employee)
        //{
        //    try
        //    {
        //        var infoUnit = EmployeeInfoUnit;
        //        infoUnit.EmployeeRepository.Insert(employee);
        //        infoUnit.EmployeeRepository.SaveChanges();
        //        return true;
        //    }
        //    catch (Exception ex)
        //    { 
        //        throw ex;
        //    }
           

            
        //}

        //public static bool UpdateEmployee(Employee employee)
        //{
        //    try
        //    {
        //        var infoUnit = EmployeeInfoUnit;
        //        infoUnit.EmployeeRepository.Update(employee);
        //        infoUnit.EmployeeRepository.SaveChanges();
        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
           
        //}

        //public static void DeleteEmployee(string EMPCode)
        //{
        //    try
        //    {
        //        var infoUnit = EmployeeInfoUnit;
        //        infoUnit.EmployeeRepository.Delete(EMPCode);
        //        infoUnit.EmployeeRepository.SaveChanges();
                
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
            
        //}


        //// 存回 下屬資料
        //public static List<Employee> GetSubordinates(string EMPCode)
        //{
        //    List<Employee> result = null;
        //    var emp = GetEmployee(EMPCode); 
        //    if (null != emp && null != emp.Subordinates)
        //        result = emp.Subordinates.ToList();

        //    return result;
        //}

        //// 存回 上司資料
        //public static List<Employee> GetSupervisors(string EMPCode)
        //{
        //    List<Employee> result = null;
        //    var emp = GetEmployee(EMPCode);  
        //    if (null != emp && null != emp.Supervisors)
        //        result = emp.Supervisors.ToList();

        //    return result;
        //}

        //#endregion



        //#region Employee Type 員工種類

        //public static EmployeeType GetEmployeeType(string EMPType)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.GetEmployeeType(EMPType);
        //}

        //public static List<EmployeeType> GetAllEmployeeTypes()
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.GetAllEmployeeTypes();
        //}

        //public static List<EmployeeType> FindEmployeeTypes(Expression<Func<EmployeeType, bool>> filter = null)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.FindEmployeeTypes(filter);
        //}

        //public static EmployeeType CreateEmployeeType(EmployeeType employeeType, uint initialSeqNumber = 0)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.CreateEmployeeType(employeeType, initialSeqNumber);
        //}

        //public static void UpdateEmployeeType(EmployeeType employeeType)
        //{
        //    AspNetEmployeeInfoProviderManager.Default.UpdateEmployeeType(employeeType);
        //}

        //public static void DeleteEmployeeType(string EMPType)
        //{
        //    AspNetEmployeeInfoProviderManager.Default.DeleteEmployeeType(EMPType);
        //}

        //#endregion



        //#region Team 小組

        //public static Team GetTeam(string TEAMCode)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.GetTeam(TEAMCode);
        //}

        //public static List<Team> GetAllTeams()
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.GetAllTeams();
        //}

        //public static List<Team> FindTeams(Expression<Func<Team, bool>> filter = null)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.FindTeams(filter);
        //}

        //public static Team CreateTeam(Team team)
        //{
        //    return AspNetEmployeeInfoProviderManager.Default.CreateTeam(team);
        //}

        //public static void UpdateTeam(Team team)
        //{
        //    AspNetEmployeeInfoProviderManager.Default.UpdateTeam(team);
        //}

        //public static void DeleteTeam(string TEAMCode)
        //{
        //    AspNetEmployeeInfoProviderManager.Default.DeleteTeam(TEAMCode);
        //}

        //#endregion


        public static EmployeeInfoUnit GetEmployeeInfoUnit()
        {
            var EF = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
            if (null != EF)
            {
                return EF.GetEmployeeInfoUnit();
            }
            else
            {
                return null;
            }
        }


        public static EmployeeInfoService GetEmployeeInfoService()
        {
            var EF = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
            if (null != EF)
            {
                return EF.GetEmployeeInfoService();
            }
            else
            {
                return null;
            }
        }


        public static EmployeeInfoService GetEmployeeInfoService(EmployeeInfoUnit unit)
        {
            return new EmployeeInfoService(unit);
        }


        public static FB.Data.Models.EmployeeInfo EmployeeSession
        {
            get
            {
                var user = System.Web.HttpContext.Current.User;
                var empInfo = System.Web.HttpContext.Current.Session["$EMPINFO"] as FB.Data.Models.EmployeeInfo;
                if (null == empInfo && user.Identity.IsAuthenticated && user.Identity is FB.Membership.Principal.FBIdentity)
                {
                    var FBI = user.Identity as FB.Membership.Principal.FBIdentity;
                    var sv = GetEmployeeInfoService();

                    var ei = sv.GetEmployeeByUserKey(FBI.UserId);

                    empInfo = (null != ei) ? ei.ToEmployeeInfo() : new Data.Models.EmployeeInfo { UserID = FBI.UserId, UserName = FBI.Name };

                    System.Web.HttpContext.Current.Session["$EMPINFO"] = empInfo;
                }

                return empInfo;
            }
        }
    }
}
